
#include "StdAfx.h"
#include "public.h"
#include "parse_xml.h"

/*
#include <memory.h>
#include <string.h>
#include <stdio.h>
*/



int GetBuffer(char * pdata, char *pbuffer, const char *pflagstr)
{
	char * ptrL = NULL,*ptrR = NULL;
	char Bflag[64],Eflag[64];
	int len;
	
	memset(Bflag,0,64);
	memset(Eflag,0,64);
	
	strcpy(Bflag,"<");
	strcat(Bflag,pflagstr);
	strcat(Bflag,">");

	strcpy(Eflag,"<");
	strcat(Eflag,"/");
	
	strcat(Eflag,pflagstr);
	strcat(Eflag,">");
	
	if(!(ptrL = strstr(pdata,Bflag))) return -1;
	if(!(ptrR = strstr(pdata,Eflag))) return -2;
	if(ptrR < ptrL) return -3;
	
	len = ptrR-ptrL;
	if(len <= 0)
	{
		return -4;
	}

	memcpy(pbuffer,ptrL +strlen(Bflag),len);
	return len ;
}

/*
<xxx


>
*/
int GetBuffer3(char * pdata, char *pbuffer, const char *pflagstr,int * pActLen)
{
	char * ptrL = NULL,*ptrR = NULL;
	char Bflag[64],Eflag[64];
	int len;
	
	memset(Bflag,0,64);
	memset(Eflag,0,64);
	
	strcpy(Bflag,"<");
	strcat(Bflag,pflagstr);
//	strcat(Bflag,">");
	
//	strcpy(Eflag,"<");
//	strcat(Eflag,"/");
	
//	strcat(Eflag,pflagstr);
	strcat(Eflag,">");
	
	if(!(ptrL = strstr(pdata,Bflag))) return -1;
	if(!(ptrR = strstr(pdata,Eflag))) return -2;
	if(ptrR < ptrL) return -3;
	
	////valid data len,include start flag string
	len = ptrR - ptrL - strlen(Bflag) ;
	
	if(len <= 0)
	{
		return -4;
	}
	* pActLen = len;
	memcpy(pbuffer,ptrL + strlen(Bflag),len);
	////currect scene offset 
	return len + strlen(Bflag) + strlen(Eflag);
}

/*
<xxx>


</xxx>
*/
int GetBuffer2(char * pdata, char *pbuffer, const char *pflagstr,int * pActLen)
{
	char * ptrL = NULL,*ptrR = NULL;
	char Bflag[64],Eflag[64];
	int len;
	
	memset(Bflag,0,64);
	memset(Eflag,0,64);
	
	strcpy(Bflag,"<");
	strcat(Bflag,pflagstr);
	strcat(Bflag,">");
	
	strcpy(Eflag,"<");
	strcat(Eflag,"/");
	
	strcat(Eflag,pflagstr);
	strcat(Eflag,">");
	
	if(!(ptrL = strstr(pdata,Bflag))) return -1;
	if(!(ptrR = strstr(pdata,Eflag))) return -2;
	if(ptrR < ptrL) return -3;
	
	////valid data len,include start flag string
	len = ptrR - ptrL - strlen(Bflag) ;
	
	if(len <= 0)
	{
		return -4;
	}
	* pActLen = len;
	memcpy(pbuffer,ptrL + strlen(Bflag),len);
	////currect scene offset 
	return len + strlen(Bflag) + strlen(Eflag);
}
/*
<xxx


</xxx>
*/
int GetBuffer1(char * pdata, char *pbuffer, const char *pflagstr,int * pActLen)
{
	char * ptrL = NULL,*ptrR = NULL;
	char Bflag[64],Eflag[64];
	int len;
	
	memset(Bflag,0,64);
	memset(Eflag,0,64);
	
	strcpy(Bflag,"<");
	strcat(Bflag,pflagstr);

	strcpy(Eflag,"<");
	strcat(Eflag,"/");
	
	strcat(Eflag,pflagstr);
	strcat(Eflag,">");
	
	if(!(ptrL = strstr(pdata,Bflag))) return -1;
	if(!(ptrR = strstr(pdata,Eflag))) return -2;
	if(ptrR < ptrL) return -3;
	
	////valid data len,include start flag string
	len = ptrR - ptrL ;
	
	if(len <= 0)
	{
		return -4;
	}
	* pActLen = len;
	memcpy(pbuffer,ptrL,len);
	////currect scene offset 
	return len + strlen(Eflag);
}

int GetSeqBuffer(char * pdata, char *pseqbuf)
{
	return GetBuffer(pdata,pseqbuf,"seq");
}

int GetStreamBuffer(char * pbuf,char * pStreamBuf,int * pLen)
{
	return GetBuffer2(pbuf,pStreamBuf,"stream",pLen);
}

int GetOneSceneData(char * pdata, char *pOutbuf,int * pLen)
{
	return 0;
}

int GetOnePaneData(char * pdata, char *pOutbuf,int * pLen)
{
	return 0;
}


int GetStartPos(char * pdata)
{
	int startpos;
	char * p;
	p = strstr(pdata,"<head");
	startpos = p - pdata;
	return startpos;
}


int GetStrIndex(char *pdata, char * pindexbuf)
{
	char *p,*pp;
	uint16 len;

	p = pdata;
	while(*p != '<') p++;
	while(*p != '>') p++;
	p++;
	len = 0;
	pp = p;
	while(*p != '<')
	{
		len++;
		p++;
	}
	memcpy(pindexbuf,pp,len);
	return len;
}

int HaveMore(char *pdata,char * pflagstr)
{
	char *ptrL,*ptrR;
	char Bflag[64],Eflag[64];

	memset(Bflag,0,64);
	memset(Eflag,0,64);

	strcpy(Bflag,"<");
	strcpy(Eflag,"<");
	strcat(Eflag,"/");
	strcat(Bflag,pflagstr);
	strcat(Eflag,pflagstr);
	strcat(Eflag,">");

	if(!(ptrL = strstr(pdata,Bflag))) return 0;
	if(!(ptrR = strstr(pdata,Eflag))) return 0;
	if(ptrR < ptrL) return 0;
	return 1;
}

int HaveImageMore(char *pdata,char * pflagstr)
{
	char *ptrL,*ptrR;
	char Bflag[64],Eflag[64];

	memset(Bflag,0,64);
	memset(Eflag,0,64);
	
	strcpy(Bflag,"<");
	strcpy(Eflag,"/");
	strcat(Bflag,pflagstr);

	strcat(Eflag,">");
	
	if(!(ptrL = strstr(pdata,Bflag))) return 0;
	if(!(ptrR = strstr(pdata,Eflag))) return 0;
	if(ptrR < ptrL) return 0;
	return 1;
}



int GetHeadBuffer(char * pdata, char *pheadbuf)
{
	return GetBuffer(pdata,pheadbuf,"head");
}


int GetQABoxBuffer(char *pdata, char *pqaboxbuf)
{
	return  GetBuffer(pdata,pqaboxbuf,"qaBox");
}

int GetOptionBuffer(char * pdata, char *pbuffer)
{
	return GetBuffer(pdata,pbuffer,"option");
}

int GetAnswerBuffer(char *pdata,char *pbuffer)
{
	return GetBuffer(pdata,pbuffer,"answer");
}

int getattrstr(char *pdata, char *pbuf,char * attrname)
{
	char *ptr,*p;
	unsigned char len = 0;

	ptr = strstr(pdata,attrname);
	if(ptr == NULL)
	{
		return -1;
	}
	ptr += strlen(attrname);  // 11: len of question-id
	while(*ptr != '"') ptr++;
	ptr++;
	p = ptr;
	while(*ptr != '"')
	{
		len++;
		*ptr++;
	}
	memcpy(pbuf,p,len);
	return len;
}


int GetQuestion(char *pdata, char *pbuf)
{
	char *ptrL,*ptrR;
	int len;

	ptrL = strstr(pdata,"<question>");
	ptrR = strstr(pdata,"</question>");
	len = ptrR-ptrL-10;
	memcpy(&ptrL[10],pbuf,len);
	return len;
}

int GetRightAnwser(char *pdata, char *pbuf)
{
	char *ptrL,*ptrR;
	uint8 len;

	ptrL = strstr(pdata,"<right-prompt>");
	ptrR = strstr(pdata,"</right-prompt>");
	len = ptrR-ptrL-10;
	memcpy(&ptrL[10],pbuf,len);
	return len;
}

int GetwrongAnwser(char *pdata, char *pbuf)
{
	char *ptrL,*ptrR;
	uint8 len;

	ptrL = strstr(pdata,"<wrong-prompt>");
	ptrR = strstr(pdata,"</wrong-prompt>");
	len = ptrR-ptrL-10;
	memcpy(&ptrL[10],pbuf,len);
	return len;
}

int GetAnswerStr(char *pdata,char *pbuf)
{
	int len = 0;
	char *p,*ptr;

	p = pdata;
	while(*p != '<') p++;
	while(*p != '>') p++;
	p++;
	ptr = p;
	while(*p != '<')
	{
		len++;
		p++;
	}
	memcpy(ptr,pbuf,len);
	return len;
	
}

int GetImageInfoSubItem(char * inputBuf,char * nReBuf)
{
	char *ptrL,*ptrR;
	int len =0;	
	ptrL = strstr(inputBuf,"<image");
	ptrR = strstr(inputBuf,"/>");
	len = ptrR-ptrL;
	if(len >=0 )
	{
		memcpy(nReBuf,inputBuf,len);
	}
	return len;
}


int GetItemFromBuf(char * pdata,char * pbuffer, const char *pflagstr)
{
	char * ptrL = NULL,*ptrR = NULL;
	char Bflag[64],Eflag[64];
	int len = -1;
	
	memset(Bflag,0,64);
	memset(Eflag,0,64);
	
	strcpy(Bflag,"<");
	strcat(Bflag,pflagstr);
	strcat(Bflag,">");

	strcpy(Eflag,"<");
	strcat(Eflag,"/");
	
	strcat(Eflag,pflagstr);
	strcat(Eflag,">");
	
	if(!(ptrL = strstr(pdata,Bflag))) return -1;
	if(!(ptrR = strstr(pdata,Eflag))) return -2;
	if(ptrR < ptrL) return -3;
	
	len = ptrR - ptrL - strlen(Bflag);
	if(len > 0)
	{
		memcpy(pbuffer,ptrL+strlen(Bflag),len);
	}
	return len;
}

int GetHeadData(char * pdata,char * nPaneHead,int *nHeadLen)
{
   return GetBuffer3(pdata,nPaneHead,"pane",nHeadLen);
}